package com.ruicar.afs.cloud.basic.common.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruicar.afs.cloud.basic.common.entity.BasicAffiliatedUnit;
import com.ruicar.afs.cloud.basic.common.entity.BasicMainInfo;
import com.ruicar.afs.cloud.basic.common.service.BasicAffiliatedUnitService;
import com.ruicar.afs.cloud.basic.common.service.BasicMainInfoService;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import com.ruicar.afs.cloud.common.modules.contract.ContractMainInfoDto;
import com.ruicar.afs.cloud.common.util.EmptyUtils;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author: Lee
 * @date: 2020/5/11 19:39
 * @description: 合同详细信息、主要信息
 */
@Slf4j
@RestController
@RequestMapping("/contractMainInfo")
@AllArgsConstructor
public class BasicMainInfoController {

    private final BasicMainInfoService basicMainInfoService;
    private final BasicAffiliatedUnitService basicAffiliatedUnitService;

    @ApiOperation(value = "根据合同号码查询合同详细信息")
    @PostMapping("/getContractDetailInfo")
    public IResponse<BasicMainInfo> getContractDetailInfo(@RequestParam("contractNo") String contractNo){
        //合同信息
        BasicMainInfo basicMainInfo = basicMainInfoService.getOne(Wrappers.<BasicMainInfo>lambdaQuery().eq(BasicMainInfo::getContractNo, contractNo));
        List<BasicAffiliatedUnit> basicAffiliatedUnits = basicAffiliatedUnitService.list(Wrappers.<BasicAffiliatedUnit>lambdaQuery().eq(BasicAffiliatedUnit::getContractNo, contractNo));
        if(EmptyUtils.isNotEmpty(basicAffiliatedUnits)&&EmptyUtils.isNotEmpty(basicAffiliatedUnits.get(0).getAffiliatedType())){
            basicMainInfo.setAffiliatedType(basicAffiliatedUnits.get(0).getAffiliatedType());
        }
        return IResponse.success(basicMainInfo);
    }

    @ApiOperation(value = "根据申请编号查询合同号码")
    @PostMapping("/findByApplyNo")
    public IResponse findByApplyNo(@RequestParam("applyNo") String applyNo) {
        List<BasicMainInfo> mains = basicMainInfoService.list(Wrappers.<BasicMainInfo>lambdaQuery()
                .eq(BasicMainInfo::getApplyNo, applyNo));
        return new IResponse().success(mains);
    }

    @ApiOperation(value = "根据合同号码查询合同主表信息")
    @PostMapping("/getContractMainInfo")
    public IResponse<BasicMainInfo> getContractMainInfo(@RequestParam("contractNo") String contractNo){
        BasicMainInfo basicMainInfo = basicMainInfoService.getOne(Wrappers.<BasicMainInfo>lambdaQuery().eq(BasicMainInfo::getContractNo, contractNo));
        ContractMainInfoDto contractMainInfoDto = JSON.parseObject(JSON.toJSONString(basicMainInfo), ContractMainInfoDto.class);
        return IResponse.success(contractMainInfoDto);
    }

}
